package com.tyning.newcode.newjz24;

//剑指 Offer 24. 反转链表
//定义一个函数，输入一个链表的头节点，反转该链表并输出反转后链表的头节点。
//
//
//
//示例:
//
//输入: 1->2->3->4->5->NULL
//输出: 5->4->3->2->1->NULL
//
//
//限制：
//
//0 <= 节点个数 <= 5000
//
//
//
//注意：本题与主站 206 题相同：https://leetcode-cn.com/problems/reverse-linked-list/


import java.util.ArrayList;

/**
 * 将链表弄成有序数组
 * 第二种方法使用的是栈
 *
 * @author haining
 */
public class Solution {

    static ArrayList<Integer> list = new ArrayList();

    public static void main(String[] args) {
        Solution solution = new Solution();
        ListNode head1 = new ListNode(1);
        ListNode head2 = new ListNode(2);
        head1.next = head2;
        ListNode head3 = new ListNode(3);
        head2.next = head3;
        ListNode head4 = new ListNode(4);
        head3.next = head4;
        ListNode head5 = new ListNode(5);
        head4.next = head5;
        solution.reverseList(head1);
    }


    public ListNode reverseList(ListNode head) {
        ListNode preNode = null;
        ListNode thisNode = head;
        if (thisNode == null || thisNode.next == null){
            return head;
        }
        while (thisNode.next != null) {
            ListNode dNode = thisNode.next;
            thisNode.next = preNode;
            preNode = thisNode;
            thisNode = dNode;
        }
        thisNode.next = preNode;
        return thisNode;
    }

    public ListNode reverseList1(ListNode head) {
        ListNode preNode = null;
        ListNode thisNode = head;
        if (thisNode == null || thisNode.next == null){
            return head;
        }
        while (thisNode != null) {
            ListNode dNode = thisNode.next;
            thisNode.next = preNode;
            preNode = thisNode;
            thisNode = dNode;
        }
        return thisNode;
    }








//    public ListNode reverseList(ListNode head) {
//        ListNode preNode = null;
//        ListNode thisNode = head;
//        while (thisNode != null) {
//            ListNode nextNode = thisNode.next;
//            thisNode.next = preNode;
//            thisNode = nextNode;
//            preNode = thisNode;
//        }
//        return thisNode;
//    }

}
